package com.owlsmart.bluecat.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.dueeeke.videoplayer.player.VideoViewManager;

/**
 * @author mingtai
 * @description
 * @time 2020/8/8 17:30
 */

public abstract class BaseActivity extends AppCompatActivity {

    public Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        setContentView(initLayout());
        initView();
        initDate();
    }

    // 封装后可以有效避免空指针异常
    // 初始化布局
    protected abstract int initLayout();

    // 初始化控件，获取控件对象
    protected abstract void initView();

    // 初始化数据，ps：监听事件，调用接口等
    protected abstract void initDate();


    // toast提示工具类
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    // toast提示工具类  异步
    public void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    public void navigateTo(Class clazz) {
        Intent intent = new Intent(mContext, clazz);
        startActivity(intent);
    }

    public void navigateToWithFlag(Class clazz,int flags) {
        Intent intent = new Intent(mContext, clazz);
        // 携带flags进行跳转
        intent.setFlags(flags);
        startActivity(intent);
    }

    /**
     * 根据key和val，把对应的值以xml形式存到本地
     *
     * @param key
     * @param val
     */
    protected void saveStringToSp(String key, String val) {
        SharedPreferences sp = getSharedPreferences("sp_ttit", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.commit();
    }

    protected String findByKey(String key) {
        SharedPreferences sp = getSharedPreferences("sp_ttit", MODE_PRIVATE);
        return sp.getString(key, "");
    }

    protected VideoViewManager getVideoViewManager() {
        return VideoViewManager.instance();
    }
}
